package Problem;

import java.util.LinkedList;

import maze.Maze;
import maze.Tile;
import Solver.Node;

public class Heuristics {

	// 1) number of cells to the goal cell.
	// 2) number of the empty cells between the mouse and the goal node.
	
	
	/**
	 * calculates the number of cells between the mouse and the exit cell
	 * by subtracting the horizontal coordinates of both cells
	 * 
	 *  returns the heuristic value of Node n
	 */
	public static int greedyHeuristic1(Node n) {
		Maze m = n.getMaze();
		int ey = m.getGoal().getY();
		int my = 0;
		LinkedList<Tile> l = m.getList();
		for (int i = 0; i < l.size(); i++) {
			if (l.get(i).isMouse()) {			
				my = l.get(i).getEnd().getY();
				break;
			}
		}
		return ey- my;		
	}
	

	/**
	 * calculates the number of cells between the mouse and the exit cell
	 * by subtracting the horizontal coordinates of both cells and then adding
	 * the path from the root node to the current node n
	 * 
	 *  returns the cost value of Node n
	 */
	public static int aStarHeuristic1(Node n) {
		// F(n) = h(n) + g(n)
		Maze m = n.getMaze();		
		int ey = m.getGoal().getY();
		int my = 0;
		LinkedList<Tile> l = m.getList();
		for (int i = 0; i < l.size(); i++) {
			if (l.get(i).isMouse()) {			
				my = l.get(i).getEnd().getY();				
				break;
			}
		}
		return (ey- my) + n.getPathCost();		
	}
	
	public static int greedyHeuristic2(Node n) {
		Maze m = n.getMaze();
		
		int my = 0;
		int ey = m.getGoal().getY();
		int ex = m.getGoal().getX();
		LinkedList<Tile> l = m.getList();
		for (int i = 0; i < l.size(); i++) {
			if (l.get(i).isMouse()) {			
				my = l.get(i).getEnd().getY();
				break;
			}
		}
		int k =0;
		int[][] board=m.getBoard();
		for (int i = my+1; i <=ey; i++) {
			
			if(board[ex][i]==0)
				k++;
			else if (board[ex][i] != 0) {
				for (int j = 0; j < m.getList().size(); j++) {
					if (m.getList().get(j).getID() == board[ex][i]) {
						Tile t = m.getList().get(j);
						if (t.getEnd().getX() + 1 < m.getHeight()
								&& board[t.getEnd().getX() + 1][i] == 0)
							k++;
						if (t.getStart().getX() - 1 > -1
								&& board[t.getStart().getX() - 1][i] == 0)
							k++;
						break;
					}

				}
			}
			}
		return k;		
	
	}
	
	public static int aStarHeuristic2(Node n){

		return greedyHeuristic2(n)+n.getPathCost();		
	

	}
}
